﻿
    layui.use(function () {
        var table = layui.table;
        $(function () {
            show();
        })
        function show() {
            var qualityNumber = $("#qualityNumber").val();
            var ingredientNumber = $("#ingredientNumber").val();
            var state = $("#state").val();
            // 先进行表格的初始渲染
            table.render({
                elem: '#RawmaterialtestingtTable',  // 表格元素的选择器
                page: {
                    limit: 5,
                    limits: [5, 8, 10]
                },
                toolbar: '#toolbarDemo',
                defaultToolbar: ['exports'],
                cols: [[
                    { type: 'checkbox', fixed: 'left', title: '复选框' },
                    { field: 'qualityNumber', title: '质检编号' },
                    { field: 'ingredientNumber', title: '原料编号' },
                    { field: 'ingredientName', title: '原料名称' },
                    { field: 'rawMaterialBatchNumber', title: '原料批次号' },
                    {
                        field: 'qualityDate', title: '质检日期', templet: function (d) {
                            return formatTime(d.qualityDate);
                        } },
                    {
                        field: 'state', title: '状态',templet: function (d) {
                            return PriorityState(d.state);
                    } },
                    {
                        field: '', title: '操作', templet: function (d) {
                            return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.rawMaterialtestingId + ')"><i class="layui-icon layui-icon-edit"></i>查看</a> ' +
                                '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.rawMaterialtestingId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>' + '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="UpdateData(' + d.rawMaterialtestingId + ')"><i class="layui-icon layui-icon-delete"></i>修改</a>';
                        }

                    }
                ]],
                // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
                data: []
            });
            $.ajax({
                url: "/Rawmaterialtesting/RawmaterialtestingShow",
                type: "get",
                data: {
                    qualityNumber: qualityNumber,
                    ingredientNumber: ingredientNumber,
                    state: state
                },
                success: function (data) {

                    data = JSON.parse(data);
                    console.log(data.data)
                    // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                    table.reload('RawmaterialtestingtTable', {
                        page: {
                            limit: 5,
                            limits: [5, 8, 10]
                        },
                        data: data.data
                    });
                },
                error: function () {
                    console.log("请求失败");
                }
            });
        }
        
        
        window.editData = function (id) {
            localStorage.setItem("rawMaterialtestingId", id)
            console.log(id)
            window.location.href = "/Rawmaterialtesting/RawmaterialtestingModelView";
        }
        window.UpdateData = function (id) {
            localStorage.setItem("rawMaterialtestingId", id)
            console.log(id)
            window.location.href = "/Rawmaterialtesting/RawmaterialtestingUpdateView";
        }
        window.deleteData = function (id) {
            $.ajax({
                url: '/Rawmaterialtesting/RawmaterialtestingDelete',
                type: 'post',
                data: { data: JSON.stringify({ rawMaterialtestingId: id }), token: localStorage.getItem("token") },
                success: function (res) {
                    var s = JSON.parse(res)
                    console.log(res)
                    if (s.data >= 1) {
                        layer.alert("删除成功");
                        setTimeout(function () {
                            location.reload();
                        }, 2000);

                    }
                    else {
                        layer.alert("删除失败");
                    }
                }
            })
        }

    });
function formatTime(timeStamp) {
        if (timeStamp) {
            var date = new Date(timeStamp);
            var year = date.getFullYear();
            var month = ('0' + (date.getMonth() + 1)).slice(-2);
            var day = ('0' + date.getDate()).slice(-2);
            var hour = ('0' + date.getHours()).slice(-2);
            var minute = ('0' + date.getMinutes()).slice(-2);
            var second = ('0' + date.getSeconds()).slice(-2);
            return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
        }
        return "";
}
function PriorityState(state) {
    debugger
    if (state == 1) {
        return "启用"
    }
    else {
        return "禁用"
    }
}
function Add() {
    debugger;
    window.location.href = "/Rawmaterialtesting/RawmaterialtestingAddView";
};

